package cn.rongdao.jrkabao;

import com.igexin.sdk.PushManager;
import com.mifengkong.frtools.app.FRApplication;
import com.mifengkong.frtools.http.request.event.CompulsoryDownLineEvent;
import com.mifengkong.frtools.util.FRDebugMode;
import com.mifengkong.frtools.util.FRSharedPreferences;
import com.mifengkong.frtools.util.FRString;
import com.squareup.leakcanary.LeakCanary;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import cn.mifengkong.appcommon.CommonApp;
import cn.mifengkong.appcommon.locate.LocationService;
import cn.mifengkong.appcommon.util.ProcessUtil;
import cn.rongdao.jrkabao.constant.SPConstant;
import cn.rongdao.jrkabao.model.CustomerServiceModel;
import cn.rongdao.jrkabao.model.VersionModel;
import cn.rongdao.jrkabao.service.GeTuiPushService;
import cn.rongdao.jrkabao.ui.login.LoginActivity;
import cn.rongdao.jrkabao.util.lifecycle.FRActivityLifecycle;
import cn.rongdao.jrkabao.util.lifecycle.UIEventBusSubscribe;

/**
 * Created by jack on 17/5/5
 */

public class App extends FRApplication {

    public LocationService mLocationService;

    @Override
    public void onCreate() {
        super.onCreate();

        //主进程
        String processName = ProcessUtil.getProcessName(this, android.os.Process.myPid());
        if (ProcessUtil.defaultProcess(BuildConfig.APPLICATION_ID, processName)) {
            registerActivityLifecycleCallbacks(new FRActivityLifecycle());
            new UIEventBusSubscribe();
            if (BuildConfig.DEBUG) {
                if (FRSharedPreferences.getStringNotClear(SPConstant.IS_TEST_OR_PRE_OR_FORMAL, "test").equals("test")) {
                    FRDebugMode.setDebugMode(true);
                } else if (FRSharedPreferences.getStringNotClear(SPConstant.IS_TEST_OR_PRE_OR_FORMAL, "pre").equals("pre")) {
                    FRDebugMode.setPreMode(true);
                }
            }

            if (LeakCanary.isInAnalyzerProcess(this)) {
                // This process is dedicated to LeakCanary for heap analysis.
                // You should not init your app in this process.
                return;
            }
            LeakCanary.install(this);

            //百度定位
            mLocationService = new LocationService(getApplicationContext());

            CommonApp.init(this, getDataMap());
            HttpServletAddress.init();

            if (!FRString.isEmpty(FRSharedPreferences.getStringNotClear(SPConstant.IS_USE_CLOUD_API, "")) || !BuildConfig.DEBUG) {
                FRDebugMode.setIsUseCloudAPiHost(true);
            }

            PushManager.getInstance().initialize(getApplicationContext(), GeTuiPushService.class);
            CustomerServiceModel.getInstance().setCustomService();
            VersionModel.getInstance().execute();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void compulsoryDownLineMessage(CompulsoryDownLineEvent compulsoryDownLineEvent) {
        LoginActivity.start(this);
    }

    private Map<String, String> getDataMap() {
        Map<String, String> map = new HashMap<>();
        //bugly
        map.put(CommonApp.CRASH_REPORT_KEY, getString(R.string.crash_app_id));
        //talkingdata
        map.put(CommonApp.TALKING_DATA_ANALYTICS_APP_ID, getString(R.string.kmao_talking_data_analytics_app_id));
        map.put(CommonApp.TALKING_DATA_TRACKING_APP_ID, getString(R.string.kmao_talking_data_tracking_app_id));
        //微信分享
        map.put(CommonApp.UMENG_SHARE_WEIXIN_ID, getString(R.string.kamao_weixin_key));
        map.put(CommonApp.UMENG_SHARE_WEIXIN_SCERET, getString(R.string.kamao_weixin_secret));
        //QQ分享
        map.put(CommonApp.UMENG_SHARE_QQZONE_ID, getString(R.string.kmao_qqzone_key));
        map.put(CommonApp.UMENG_SHARE_QQZONE_SCERET, getString(R.string.kamao_qqzone_secret));
        //微博分享
        map.put(CommonApp.UMENG_SHARE_SINAWEIBO_ID, getString(R.string.kmao_weibo_key));
        map.put(CommonApp.UMENG_SHARE_SINAWEIBO_SCERET, getString(R.string.kmao_weibo_secret));
        map.put(CommonApp.UMENG_SHARE_SINAWEIBO_LINK, getString(R.string.kmao_weibo_link));

        return map;
    }
}